<html>
<head>
<style>
.sample {
    font-family: monospace;
    font-size: 24px;
    margin: 20px;
    padding: 10px;
}
</style>
<script>
var expectedValues = ['0,0'];
var messages = ['start'];
function checkSelection()
{
    debug(messages.shift());
    shouldBeEqualToString('selection()', expectedValues.shift());
}

function mouseMoveTo(x, y, direction, expected)
{
    expectedValues.push(expected);
    messages.push(direction);
    eventSender.mouseMoveTo(x, y);
}

function selection()
{
    var field = document.getElementById('target');
    return field.selectionStart + ',' + field.selectionEnd;
}

function calculateFieldX(x)
{
    var target = document.getElementById('target');
    var work = document.getElementById('work');
    work.innerHTML = target.value;
    var range = document.createRange();
    range.setStart(work.firstChild, x);
    var rect = range.getClientRects()[0];
    return target.offsetLeft + rect.left - work.offsetLeft;
}

function testIt(behavior)
{
    internals.settings.setEditingBehavior(behavior);

    var macOrUnix = behavior == 'mac' || behavior == 'unix';
    var field = document.getElementById('target');
    var middleX = calculateFieldX(field.value.length / 2);
    var middleY = field.offsetTop + field.offsetHeight / 2;

    field.selectionStart = 0;
    field.selectionEnd = 0;

    expectedValues = ['0,0'];
    messages = ['start ' + behavior];
    document.addEventListener('mousemove', checkSelection);

    mouseMoveTo(calculateFieldX(6), middleY, 'move at start of word', '6,6');
    eventSender.mouseDown();
    mouseMoveTo(calculateFieldX(11), middleY, 'move to end of word', '6,11');
    eventSender.mouseUp();
    eventSender.mouseDown();
    mouseMoveTo(0, middleY, 'move to left', '0,6');
    mouseMoveTo(document.body.offsetWidth, middleY, 'move to right', '6,17');
    mouseMoveTo(middleX, middleY - 2 * field.offsetHeight, 'way up', macOrUnix ? '0,6' : '6,8');
    mouseMoveTo(middleX, middleY - field.offsetHeight, 'move to up', macOrUnix ? '0,6' : '6,8');
    mouseMoveTo(middleX, middleY + field.offsetHeight, 'move to down', macOrUnix ? '6,17' : '6,8');
    mouseMoveTo(middleX, middleY + 2 * field.offsetHeight, 'way down', macOrUnix ? '6,17' : '6,8');
    eventSender.mouseUp();
    checkSelection();
    debug('finish ' + behavior);
    shouldBeEqualToString('selection()', macOrUnix ? '6,17' : '6,8');

    document.removeEventListener('mousemove', checkSelection);
}
</script>
</head>
<body>
<div id="container">
    <p>
        This is a test for <i><a href="https://bugs.webkit.org/show_bug.cgi?id=9312">http://bugzilla.opendarwin.org/show_bug.cgi?id=9312</a>
        REGRESSION: Selection bug in new text fields when selecting past the first letter</i>.
    </p>
    <hr>
    <p>
        Curabitur pretium, quam quis semper malesuada, est libero feugiat libero, vel fringilla orci nibh sed neque. Quisque eu nulla non nisi molestie accumsan. Etiam tellus urna, laoreet ac, laoreet non, suscipit sed, sapien. Phasellus vehicula, sem at posuere vehicula, augue nibh molestie nisl, nec ullamcorper lacus ante vulputate pede.
    </p>
    <input id="target" value="Lorem ipsum dolor" class="sample">
    <p>
        Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus. Mauris viverra augue vitae purus. Morbi sed sem. Donec dui nisi, ultrices non, pretium quis, hendrerit non, est. Donec tellus. Donec eget dui id eros pharetra rutrum. Suspendisse sodales lectus sit amet nulla. Morbi tortor arcu, convallis blandit, elementum eu, aliquet a, tellus.
    </p>
    <div id="work" class="sample"></div>
</div>
<div id="console"></div>
<script src="../../resources/js-test.js"></script>
<script>
if (window.testRunner) {
    testIt('mac');
    testIt('unix');
    testIt('win');
    document.getElementById('container').outerHTML = '';
}
</script>
</body>
</html>
